public class BrinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    /**
     * 查找操作
     */
    public TreeNode search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val == val) {
                return cur;
            } else if (cur.val < val) {
                cur = cur.right;
            } else {
                cur = cur.left;
            }
        }
        return null;
    }

    /**
     * 插入操作
     */
    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        //先构建出这个节点再插入
        if (root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else {
                //搜索树中已经有相同的元素
                //此时不能进行插入
                return false;
            }
        }
        //此时找到合适的叶子节点位置
        if (parent.val > val) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }

    /**
     * 删除值为key的节点
     */
    private void remove(TreeNode cur, TreeNode parent) {
        //第一种情况 cur的左树为空
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        }//第二种情况 cur的右数为空
        else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (parent.left == cur) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        }//第三种情况 替罪羊删除法
        else {
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            //我在这里是找右树的最小值
            while (target.left != null){
                targetParent = target;
                target = target.left;
            }
            //此时找到了要当替罪羊的那个节点
            //保存在target中
            cur.val = target.val;
            //覆盖掉cur节点的值 此时删除target节点
            //下面判断我们找的是右树的最小值还是左树的最大值
            if(target == targetParent.left){
                //如果是右树的最小值
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

    public void removeNode(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                remove(cur, parent);
                return;
            }
        }
    }
}












































